<?php

namespace App\Model\Guild;

use App\Model\UserProfile;
use App\Util\ComUtil;
use App\Util\YzhUtil;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class GuildWithdrawal extends GuildBase
{
    //待审核
    const STATUS_ING = 0;
    //审核通过
    const STATUS_OK = 1;
    //审核不通过
    const STATUS_FAIL = 2;
    //云账户提现异常
    const STATUS_YZH_ERR = 3;

    const ACCOUNT_TYPE = [
        1 => '银行卡',
        2 => '支付宝',
        0 => '未知'
    ];

    const SUBJECT = [
        0 => '心友',
        1 => '芯米'
    ] ;

    //状态
    const STATUS = [
        self::STATUS_ING => '待处理',
        self::STATUS_OK => '已提现',
        self::STATUS_FAIL => '不允许提现',
        self::STATUS_YZH_ERR => '提现异常'
    ];

    //银行提现
    const WITHDRAW_TYPE_BANK = 1;

    //支付宝提现
    const WITHDRAW_TYPE_ALI = 2;

    const WITHDRAW_WAY_YZH = 1;

    //当日到账
    const DAY_SERVICE = 1;

    //下个结算日
    const ROUTINE_SERVICE = 2;


    const XIN_YOU_SUBJECT = 'yzh';

    const XIN_MI_SUBJECT = 'yzh_xmi';

    const WITHDRAW_WAY = [
        self::WITHDRAW_WAY_YZH => '云账户',
    ];

    const TO_ACCOUNT_TYPE = [
        self::DAY_SERVICE => '当日到账',
        self::ROUTINE_SERVICE => '常规到账'
    ];

    const TO_ACCOUNT_SUBJECT = [
        self::XIN_YOU_SUBJECT => '心友',
        self::XIN_MI_SUBJECT => '芯米'
    ];

    //表名字
    protected $table = 'guild_withdrawal';

    /**
     * @return BelongsTo
     */
    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    /**
     * @return BelongsTo
     */
    public function guildProfile()
    {
        return $this->belongsTo(GuildManage::class, 'user_id', 'user_id');
    }

    /**
     * 提现主题
     * @param string $type
     * @return string
     */
    static function getSubject(string $type = ''): string
    {
        return self::SUBJECT[$type] ?? '未知';
    }

    /**
     * 提现账户
     * @param string $type
     * @return string
     */
    static function getWithdrawWay(string $type = ''): string
    {
        return self::WITHDRAW_WAY[$type] ?? '未知';
    }

    /**
     * 到账账户类型
     * @param string $type
     * @return string
     */
    static function getAccountType(string $type = ''): string
    {
        return self::ACCOUNT_TYPE[$type] ?? '未知';
    }

    /**
     * 通知云账户下单
     * @param $withdrawRecord
     * @param string $accountType
     * @return array|false
     */
    static function doYzhOrder($withdrawRecord = null, string $accountType='')
    {
        if (!$withdrawRecord) return false;
        $userId = $withdrawRecord['user_id'];
        $payRmb = $withdrawRecord['reality_rmb'];

        $orderInfo = [
            'pay_money' => $payRmb,
            'id_card' => $withdrawRecord['withdraw_id_card'],
            'real_name' => $withdrawRecord['withdraw_real_name'],
            'card_no' => $withdrawRecord['withdraw_account'],
            'notify_url' => config('yzh_xy.notify_url'),
            'order_id' => $orderId = ComUtil::buildOrderId($userId),
        ];
        //是否支付宝
        $bindAliAccount = self::WITHDRAW_TYPE_ALI == $withdrawRecord['withdraw_type'];

        //更新云账户订单ID
        GuildWithdrawal::where('id', $withdrawRecord['id'])->update([
            'yzh_order_id' => $orderId,
            //银行卡
            'account_type' => $bindAliAccount ? 0 : 1,
            'subject' => $accountType == 'yzh_xmi' ? 1 : 0,
        ]);

        if ($bindAliAccount) {
            $result = YzhUtil::orderAlipay($orderInfo,$accountType);
        } else {
            $result = YzhUtil::orderBank($orderInfo,$accountType);
        }
        if (isset($result['code']) && $result['code'] == '0000') {
            return ['ret' => 0, 'message' => '已成功提交提现请求', 'bind_type' => $withdrawRecord['withdraw_type']];
        }
        return ['ret' => -1, 'message' => "{$result['message']}; ErrCode:{$result['code']}", 'bind_type' => $withdrawRecord['withdraw_type']];
    }

    public static function getTotalHistoryWithdrawalByUserId($userId){
        return self::where('user_id',$userId)->where('status',self::STATUS_OK)->sum('withdraw_rmb') ?? 0;
    }

    public static function getTotalBeingWithdrawalByUserId($userId){
        return self::where('user_id',$userId)->where('status',self::STATUS_ING)->sum('withdraw_rmb') ?? 0;
    }
}
